/**
 * 时间戳+随机6位数生成ID
 * @author：mashuai
 */
export const nextIdSys = () => {
    return new Date().getTime() + Math.round(Math.random() * 100000000, 0);
}

/***
 * 获取url后面的参数值
 * @param {*} name 需要获取的key 
 * @author：mashuai
 */
export function getUrlKey(name) {
    return (
        decodeURIComponent(
            (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
                location.href
            ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
    );
}


/***
 * 获取new Date()的天
 */

export function getTimeDay(date) {
    let day = date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString();
    return day;
}
/***
 * 获取new Date()的月
 */
export function getTimeMonth(date) {
    let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString(); //'04'
    return month;
}
/***
 * 获取new Date()的年
 */

export function getTimeYear(date) {
    let year = date.getFullYear().toString();
    return year;
}


/**
 * 默认时间范围,调用此方法，给平台时间框赋默认时间范围
 * @param {*} type 
 * type = m 默认当前月时间范围 2023-05-01至2023-05-当天，
 * type = y 默认当前年时间范围，2023-05至2023-当月
 * type = d 默认当日 
 */
export function defTimeRange(type) {
    let date = new Date();
    let year = getTimeYear(date);
    let month = getTimeMonth(date);
    let day = getTimeDay(date);
    if (type == 'm') {
        return year + "-" + month + "-" + "01" + "," + year + "-" + month + "-" + day;
    } else if (type == 'y') {
        return year + "-" + "01" + "," + year + "-" + month;
    } else if (type == 'd') {
        return year + "-" + month + "-" + day + "," + year + "-" + month + "-" + day
    }
    return null;
}

/**
 * 根据某个组件，向上找到最近的指定组件（父组件）
 * @param {*} context 当前上下文，基于某组件来向上寻找，一般都是基于当前的组件，也就是传this
 * @param {*} componentName 要找的组件的name
 * @returns 找到的组件实例
 */
export const findComponentUpward = (context, componentName) => {
    let parent = context.$parent;
    let name = parent.$options.name;

    while (parent && (!name || [componentName].indexOf(name) < 0)) {
        parent = parent.$parent;
        if (parent) name = parent.$options.name;
    }
    return parent;
}

/**
 * 根据url下载文件
 * @param {*} url
 */
export const download = (url) => {
    fetch(url).then(res => res.blob()).then(blob => {
        let a = document.createElement('a');
        let blobUrl = window.URL.createObjectURL(blob);
        a.href = blobUrl;
        // 文件名
        a.download = url.substring(url.lastIndexOf('/') + 1, url.length);
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(blobUrl);
    });
}

/**
 * 获取某个对象在数组中的下标
 * @param {arr} 数组
 * @param {obj} 对象
 */
export const getArrayIndex = (arr, obj) => {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return i;
        }
    }
    return -1;
}

/**
 * 时间格式转换
 * @param {Date} date 时间 
 * @param {String} format 格式
 * @returns 
 */
export const formatDate = (date,format) => {
    const year = date.getFullYear();
    const month = ('0' + (date.getMonth() + 1)).slice(-2);
    const day = ('0' + date.getDate()).slice(-2);
    const hours = ('0' + date.getHours()).slice(-2);
    const minutes = ('0' + date.getMinutes()).slice(-2);
    const seconds = ('0' + date.getSeconds()).slice(-2);
    if (format == "yyyy-MM-dd") {
        return `${year}-${month}-${day}`;
    } else {
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
}